﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using SignalR;
using SignalR.Hubs;


namespace SignalRHubs
{
    public class ChatHub : Hub
    {
        // List of connected Phone clients on server .. feel free to persist or use this list however.
        private static List<PhoneClient> PhoneClientList = new List<PhoneClient>();

        // SignalR method call to add a new Phone client connection & join chatroom.
        public void JoinFromPhone(string phoneID, string chatUserName)
        {
            PhoneClient phoneClientToAdd = new PhoneClient();
            phoneClientToAdd.PhoneClientId = phoneID;
            PhoneClientList.Add(phoneClientToAdd);           

            // Guess what this does?
            // AddToGroup("ChatRoom A");
        }

        // Disconnect given Phone client & leave chatroom.
        public void Disconnect(string phoneID, string chatUserName)
        {
            PhoneClient client = null;

            foreach (PhoneClient existingClient in PhoneClientList)
            {
                if (phoneID == existingClient.PhoneClientId)
                {
                    client = existingClient;
                    break;
                }
            }

            // Cleanup & remove from chatroom.
            PhoneClientList.Remove(client);
            Clients.removeClient(Caller);

            // Pop out of group.
            // RemoveFromGroup("ChatRoom A");
        }

        // Get chatty.
        public void PushMessageToClients(string message)
        {
            // Push to all connected clients.
            Clients.addChatMessage(message);


            // Guess what the next few lines do ...
            
            // Invoke a method on the calling client only.
            // Caller.addChatMessage(message);

            // Similar to above, the more verbose way.
            // Clients[Context.ConnectionId].addChatMessage(message);

            // Communicate to a Group.
            // Clients["ChatRoom A"].addChatMessage(message);
        }
    }
}